package com.jerry.passenger.account.presenter;


import com.jerry.passenger.account.model.IUserManager;
import com.jerry.passenger.account.model.response.LoginResponse;
import com.jerry.passenger.account.model.response.RegisterResponse;
import com.jerry.passenger.account.view.ICreatePasswordDialogView;
import com.jerry.passenger.common.databus.RegisterBus;

public class CreatePasswordDialogPresenterImpl implements ICreatePasswordDialogPresenter {

    private ICreatePasswordDialogView view;
    private IUserManager userManager;

    /**
     * 接收子线程消息
     */
    @RegisterBus
    public void onRegisterResponse(RegisterResponse registerResponse) {
        // 处理UI 变化
        switch (registerResponse.getCode()) {
            case IUserManager.REGISTER_SUC:
                // 注册成功
                view.showRegisterSuc();
                break;
            case IUserManager.SERVER_FAIL:
                // 服务器错误
                view.showError(IUserManager.SERVER_FAIL, "");
                break;
        }
    }

    @RegisterBus
    public void onLoginResponse(LoginResponse LoginResponse) {
        // 处理UI 变化
        switch (LoginResponse.getCode()) {
            case IUserManager.LOGIN_SUC:
                // 登录成功
                view.showLoginSuc();
                break;
            case IUserManager.SERVER_FAIL:
                // 服务器错误
                view.showError(IUserManager.SERVER_FAIL, "");
                break;
        }

    }

    /**
     * 注入 view 和 accountManager 对象
     *
     * @param view
     * @param userManager
     */
    public CreatePasswordDialogPresenterImpl(ICreatePasswordDialogView view,
                                             IUserManager userManager) {
        this.view = view;
        this.userManager = userManager;
    }

    /**
     * 校验密码输入合法性
     *
     * @param pw
     * @param pw1
     */
    @Override
    public boolean checkPw(String pw, String pw1) {
        if (pw == null || pw.equals("")) {
            view.showPasswordNull();
            return false;
        }
        if (!pw.equals(pw1)) {
            view.showPasswordNotEqual();
            return false;
        }
        return true;
    }

    /**
     * 注册
     *
     * @param phone
     * @param pw
     */
    @Override
    public void requestRegister(String phone, String pw) {
        userManager.register(phone, pw);
    }

    /**
     * 登录
     *
     * @param phone
     * @param pw
     */
    @Override
    public void requestLogin(String phone, String pw) {
        userManager.login(phone, pw);
    }

}
